var faces = {
    "[微笑]": "/index/layui/images/face/0.gif",
    "[嘻嘻]": "/index/layui/images/face/1.gif",
    "[哈哈]": "/index/layui/images/face/2.gif",
    "[可爱]": "/index/layui/images/face/3.gif",
    "[可怜]": "/index/layui/images/face/4.gif",
    "[挖鼻]": "/index/layui/images/face/5.gif",
    "[吃惊]": "/index/layui/images/face/6.gif",
    "[害羞]": "/index/layui/images/face/7.gif",
    "[挤眼]": "/index/layui/images/face/8.gif",
    "[闭嘴]": "/index/layui/images/face/9.gif",
    "[鄙视]": "/index/layui/images/face/10.gif",
    "[爱你]": "/index/layui/images/face/11.gif",
    "[泪]": "/index/layui/images/face/12.gif",
    "[偷笑]": "/index/layui/images/face/13.gif",
    "[亲亲]": "/index/layui/images/face/14.gif",
    "[生病]": "/index/layui/images/face/15.gif",
    "[太开心]": "/index/layui/images/face/16.gif",
    "[白眼]": "/index/layui/images/face/17.gif",
    "[右哼哼]": "/index/layui/images/face/18.gif",
    "[左哼哼]": "/index/layui/images/face/19.gif",
    "[嘘]": "/index/layui/images/face/20.gif",
    "[衰]": "/index/layui/images/face/21.gif",
    "[委屈]": "/index/layui/images/face/22.gif",
    "[吐]": "/index/layui/images/face/23.gif",
    "[哈欠]": "/index/layui/images/face/24.gif",
    "[抱抱]": "/index/layui/images/face/25.gif",
    "[怒]": "/index/layui/images/face/26.gif",
    "[疑问]": "/index/layui/images/face/27.gif",
    "[馋嘴]": "/index/layui/images/face/28.gif",
    "[拜拜]": "/index/layui/images/face/29.gif",
    "[思考]": "/index/layui/images/face/30.gif",
    "[汗]": "/index/layui/images/face/31.gif",
    "[困]": "/index/layui/images/face/32.gif",
    "[睡]": "/index/layui/images/face/33.gif",
    "[钱]": "/index/layui/images/face/34.gif",
    "[失望]": "/index/layui/images/face/35.gif",
    "[酷]": "/index/layui/images/face/36.gif",
    "[色]": "/index/layui/images/face/37.gif",
    "[哼]": "/index/layui/images/face/38.gif",
    "[鼓掌]": "/index/layui/images/face/39.gif",
    "[晕]": "/index/layui/images/face/40.gif",
    "[悲伤]": "/index/layui/images/face/41.gif",
    "[抓狂]": "/index/layui/images/face/42.gif",
    "[黑线]": "/index/layui/images/face/43.gif",
    "[阴险]": "/index/layui/images/face/44.gif",
    "[怒骂]": "/index/layui/images/face/45.gif",
    "[互粉]": "/index/layui/images/face/46.gif",
    "[心]": "/index/layui/images/face/47.gif",
    "[伤心]": "/index/layui/images/face/48.gif",
    "[猪头]": "/index/layui/images/face/49.gif",
    "[熊猫]": "/index/layui/images/face/50.gif",
    "[兔子]": "/index/layui/images/face/51.gif",
    "[ok]": "/index/layui/images/face/52.gif",
    "[耶]": "/index/layui/images/face/53.gif",
    "[good]": "/index/layui/images/face/54.gif",
    "[NO]": "/index/layui/images/face/55.gif",
    "[赞]": "/index/layui/images/face/56.gif",
    "[来]": "/index/layui/images/face/57.gif",
    "[弱]": "/index/layui/images/face/58.gif",
    "[草泥马]": "/index/layui/images/face/59.gif",
    "[神马]": "/index/layui/images/face/60.gif",
    "[囧]": "/index/layui/images/face/61.gif",
    "[浮云]": "/index/layui/images/face/62.gif",
    "[给力]": "/index/layui/images/face/63.gif",
    "[围观]": "/index/layui/images/face/64.gif",
    "[威武]": "/index/layui/images/face/65.gif",
    "[奥特曼]": "/index/layui/images/face/66.gif",
    "[礼物]": "/index/layui/images/face/67.gif",
    "[钟]": "/index/layui/images/face/68.gif",
    "[话筒]": "/index/layui/images/face/69.gif",
    "[蜡烛]": "/index/layui/images/face/70.gif",
    "[蛋糕]": "/index/layui/images/face/71.gif"
};

function escape(html) {
    return String(html || '').replace(/&(?!#?[a-zA-Z0-9]+;)/g, '&amp;')
        .replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/'/g, '&#39;').replace(/"/g, '&quot;');
}

function content(content) {
    //支持的html标签
    var html = function (end) {
        return new RegExp('\\n*\\|\\-' + (end || '') + '(div|span|p|button|table|thead|th|tbody|tr|td|ul|li|ol|li|dl|dt|dd|h2|h3|h4|h5)([\\s\\S]*?)\\-\\|\\n*', 'g');
    };
    //XSS
    content = escape(content || '')

    //转义图片
        .replace(/img\[([^\s]+?)\]/g, function (img) {
            return '<img src="' + img.replace(/(^img\[)|(\]$)/g, '') + '">';
        })

        //转义@
        .replace(/@(\S+)(\s+?|$)/g, '@<a href="javascript:;" class="fly-aite">$1</a>$2')
        //转义表情
        .replace(/face\[([^\s\[\]]+?)\]/g, function(face){
            var alt = face.replace(/^face/g, '');
            return '<img alt="'+ alt +'" title="'+ alt +'" src="' + faces[alt] + '">';
        })
        //转义脚本
        .replace(/a(\(javascript:)(.+)(;*\))/g, 'a(javascript:layer.msg(\'非法脚本\');)')

        //转义链接
        .replace(/a\([\s\S]+?\)\[[\s\S]*?\]/g, function (str) {
            var href = (str.match(/a\(([\s\S]+?)\)\[/) || [])[1];
            var text = (str.match(/\)\[([\s\S]*?)\]/) || [])[1];
            if (!href) return str;
            var rel = /^(http(s)*:\/\/)\b(?!(\w+\.)*(sentsin.com|layui.com))\b/.test(href.replace(/\s/g, ''));
            return '<a href="' + href + '" target="_blank"' + (rel ? ' rel="nofollow"' : '') + '>' + (text || href) + '</a>';
        })

        //转义横线
        .replace(/\[hr\]\n*/g, '<hr>')

        //转义表格
        .replace(/\[table\]([\s\S]*)\[\/table\]\n*/g, function (str) {
            return str.replace(/\[(thead|th|tbody|tr|td)\]\n*/g, '<$1>')
                .replace(/\n*\[\/(thead|th|tbody|tr|td)\]\n*/g, '</$1>')
                .replace(/\[table\]\n*/g, '<table class="layui-table">')
                .replace(/\n*\[\/table\]\n*/g, '</table>');
        })

        //转义 div/span
        .replace(/\n*\[(div|span)([\s\S]*?)\]([\s\S]*?)\[\/(div|span)\]\n*/g, function (str) {
            return str.replace(/\[(div|span)([\s\S]*?)\]\n*/g, '<$1 $2>')
                .replace(/\n*\[\/(div|span)\]\n*/g, '</$1>');
        })

        //转义列表
        .replace(/\[ul\]([\s\S]*)\[\/ul\]\n*/g, function (str) {
            return str.replace(/\[li\]\n*/g, '<li>')
                .replace(/\n*\[\/li\]\n*/g, '</li>')

                .replace(/\[ul\]\n*/g, '<ul>')
                .replace(/\n*\[\/ul\]\n*/g, '</ul>');
        })

        //转义代码
        .replace(/\[pre\]([\s\S]*)\[\/pre\]\n*/g, function (str) {
            return str.replace(/\[pre\]\n*/g, '<pre>')
                .replace(/\n*\[\/pre\]\n*/g, '</pre>');
        })

        //转义引用
        .replace(/\[quote\]([\s\S]*)\[\/quote\]\n*/g, function (str) {
            return str.replace(/\[quote\]\n*/g, '<div class="layui-elem-quote">')
                .replace(/\n*\[\/quote\]\n*/g, '</div>');
        })

        //转义换行
        .replace(/\n/g, '<br>')

    return content;
}